//
//  AppDelegate+common.m
//  XMJJ_zml
//
//  Created by apple on 2025/5/7.
//

#import "AppDelegate+common.h"

@implementation AppDelegate (common)

+ (UIViewController *)getCurrentVC {
    UIViewController * rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
    
    UIViewController * currentVC = [self getCurrentVCFromRootVC:rootViewController];
    return currentVC;
}

+ (UIViewController *)getCurrentVCFromRootVC:(UIViewController *)rootVC {
    UIViewController * currentVC;
    if ([rootVC presentedViewController]) {
        // 视图是被presented出来的
        rootVC = [rootVC presentedViewController];
    }
    if ([rootVC isKindOfClass:[UITabBarController class]]) {
        // 根视图为UITabBarController
        currentVC = [self getCurrentVCFromRootVC:[(UITabBarController *)rootVC selectedViewController]];
        
    }
    else if ([rootVC isKindOfClass:[UINavigationController class]]) {
        // 根视图为UINavigationController
        currentVC = [self getCurrentVCFromRootVC:[(UINavigationController *)rootVC visibleViewController]];
        
    }
    else {
        // 根视图为非导航类
        currentVC = rootVC;
    }
    return currentVC;
}

@end
